package com.gleason.gt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.gleason.gt.adapters.PlaylistAdapter;
import com.gleason.gt.server.model.database.PlayListEntry;
import com.thoughtworks.xstream.XStream;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class PlayListActivity extends ListActivity {
	private PlayListEntry selectedPlaylistEntry;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playlist);
		List<PlayListEntry> entries = callWebService();
		PlaylistAdapter pa = new PlaylistAdapter(this,entries);
		setListAdapter(pa);
		
	}
    @SuppressWarnings("unchecked")
	public List<PlayListEntry> callWebService(){
    	List<PlayListEntry> pl = null;
    	HttpClient httpclient = new DefaultHttpClient();
		HttpGet request = new HttpGet(getString(R.string.get_playlist_url));
		try {
			HttpResponse response = httpclient.execute(request);
			BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		    StringBuffer sb = new StringBuffer("");
		    String line = "";
		    while ((line = in.readLine()) != null) {
		    	sb.append(line);
		    }
		    in.close();
		    String entriesText = sb.toString();
		    pl = (List<PlayListEntry>) new XStream().fromXML(entriesText);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		httpclient.getConnectionManager().shutdown();
		return pl;
    } // end callWebService()
    
}
